/*
 * @Company: TSBVN
 * @Author: marcusbui
 * @Revision: May 12, 2011
 */
package tsb.com.html5.comment.service;

import java.util.List;

import javax.annotation.Resource;

import tsb.com.html5.base.BaseService;
import tsb.com.html5.comment.dao.ICommentDao;
import tsb.com.html5.comment.item.CommentItem;
import tsb.com.html5.comment.param.GetCommentParam;
import tsb.com.html5.comment.param.ProcessCommentParam;

/**
 * The Class CommentService.
 */
public class CommentService extends BaseService implements ICommentService {
	private static final long serialVersionUID = 1L;
	
	@Resource(name="commentDao")
	private ICommentDao commentDao;

	public List<CommentItem> getComment(GetCommentParam parm) throws Exception {
		try{
			List<CommentItem> objectList = (List<CommentItem>) commentDao.getAllComment(parm);
			return objectList;	
		}catch(Exception e){
			throw e;
		}
	}
	
	public CommentItem getCommentById(GetCommentParam parm) throws Exception {
		CommentItem returnItem = null;
		try{
			List<CommentItem> returnItems = (List<CommentItem>) commentDao.getCommentByParam(parm);
			if(returnItems!=null && returnItems.size()>0){
				returnItem = returnItems.get(0);
			}
			
		}catch(Exception e){
			throw e;
		}
		return returnItem;
	}

	public boolean processComment(ProcessCommentParam parm) throws Exception {
		boolean success = true;
		try{
			List<CommentItem> commentList = parm.getCommentList();
			if(commentList!=null && commentList.size()>0){
				for (CommentItem commentItem : commentList) {
					if(commentItem.getCrud().equals("C")){
						commentDao.insertComment(commentItem);
					}else if(commentItem.getCrud().equals("D")){
						commentDao.deleteComment(commentItem);
					}else if(commentItem.getCrud().equals("U")){
						commentDao.updateComment(commentItem);
					}
				}
			}
		}catch(Exception e){
			success = false;
			throw e;
		}
		return success;
	}

	public CommentItem createComment(CommentItem parm) throws Exception {
		CommentItem createdItem = null;
		try{
			createdItem = (CommentItem)commentDao.insertComment(parm);
		}catch(Exception e){
			throw e;
		}
		return createdItem;
	}

	public boolean deleteComment(CommentItem parm) throws Exception {
		boolean success = false;
		try{
			int affectedRow = commentDao.deleteComment(parm);
			if(affectedRow>0){
				success=true;
			}
		}catch(Exception e){
			throw e;
		}
		return success;
	}
	
	public CommentItem updateComment(CommentItem parm) throws Exception {
		CommentItem updatedItem = null;
		try{
			updatedItem = (CommentItem) commentDao.updateComment(parm);
		}catch(Exception e){
			throw e;
		}
		return updatedItem;
	}
	
}
